package com.njtoyo.taxi.admin.service.data.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njtoyo.taxi.admin.handler.FilterHandler;
import com.njtoyo.taxi.admin.library.Const;
import com.njtoyo.taxi.admin.mapper.master.entity.Company;
import com.njtoyo.taxi.admin.mapper.master.mapper.CompanyMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.TaxiMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.UserMapper;
import com.njtoyo.taxi.admin.mapper.secondary.entity.data.DashboardOrderDaily;
import com.njtoyo.taxi.admin.mapper.secondary.enums.DataOrderType;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.data.DashboardOrderDailyMapper;
import com.njtoyo.taxi.admin.rest.presenter.data.data_analysis.WholeDataPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.data.data_analysis.WholeDataWrapper;
import com.njtoyo.taxi.admin.service.data.WholeService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@DS(Const.PRIMARY_DS)
public class WholeServiceImpl implements WholeService {

    @Autowired
    private TaxiMapper taxiMapper;

    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private DashboardOrderDailyMapper dashboardOrderDailyMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public RestResult<WholeDataPresenter> mainData(AdminUser adminUser, WholeDataWrapper wrapper) {
        WholeDataPresenter res = new WholeDataPresenter();
        FilterHandler filterHandler = new FilterHandler(adminUser, wrapper.getCantonId(), wrapper.getCompanyId());
        // company
        if (adminUser.getResourceNames().contains("data_analyse_all_company")) {
            res.setCompany(getCompanyData(filterHandler));
        }
        // 订单
        if (adminUser.getResourceNames().contains("data_analyse_all_order")) {
            res.setOrder(getOrderData(filterHandler));
        }
        // 乘客
        if (adminUser.getResourceNames().contains("data_analyse_all_customer")) {
            res.setCustomer(getCustomerData(filterHandler));
        }
        // 司机
        if (adminUser.getResourceNames().contains("data_analyse_all_driver")) {
            res.setDriver(getDriverData(filterHandler));
        }

        if (adminUser.getResourceNames().contains("data_analyse_all_taxi")) {
            // 车辆总数
            res.setTaxi(getTaxiData(filterHandler));
            // 激活车辆
            res.setActiveTaxi(getActiveTaxiData(filterHandler));
        }

        return RestResult.success(res);
    }

    private Long getCompanyData(FilterHandler filterHandler) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        switch (filterHandler.getFilterType()) {
            case canton:
                queryWrapper.in("canton_id", filterHandler.getCantonIds());
                break;
            case company:
                queryWrapper.in("id", filterHandler.getCompanyIds());
                break;
        }
        long total = Long.parseLong(companyMapper.selectCount(queryWrapper).toString());

        // 翻倍率
//        Double ride = filterHandler.getFakeMultiple().getRide();
//        if (Objects.nonNull(ride)) {
//            total = (long) (total * ride);
//        }
        return total;
    }

    /**
     * 订单数
     */
    private Long getOrderData(FilterHandler filterHandler) {
        QueryWrapper<DashboardOrderDaily> queryWrapper = new QueryWrapper<>();
        switch (filterHandler.getFilterType()) {
            case canton:
                queryWrapper.in("canton_id", filterHandler.getCantonIds());
                break;
            case company:
                queryWrapper.in("company_id", filterHandler.getCompanyIds());
                break;
        }
        queryWrapper.in("is_imitate", filterHandler.getIsImitates())
                .in("type", DataOrderType.pickOnline, DataOrderType.pickScan, DataOrderType.simpleCall, DataOrderType.phoneCall)
                .select("ifnull(SUM(num), 0)");
        long total = Long.parseLong(dashboardOrderDailyMapper.selectObjs(queryWrapper).get(0).toString());

        // 翻倍率
        Double ride = filterHandler.getFakeMultiple().getRide();
        if (Objects.nonNull(ride)) {
            total = (long) (total * ride);
        }
        return total;
    }

    /**
     * 车俩
     */
    private Long getTaxiData(FilterHandler filterHandler) {
        Long total = 0L;
        switch (filterHandler.getFilterType()) {
            case platform:
                total = taxiMapper.getTotalCount(null, null);
                break;
            case canton:
                total = taxiMapper.getTotalCount(filterHandler.getCantonIds(), null);
                break;
            case company:
                total = taxiMapper.getTotalCount(null, filterHandler.getCompanyIds());
        }
        // 翻倍率
        Double taxi = filterHandler.getFakeMultiple().getRegisterTotalTaxi();
        if (Objects.nonNull(taxi)) {
            total = (long) (total * taxi);
        }
        return total;
    }

    /**
     * 激活车辆
     */
    private Long getActiveTaxiData(FilterHandler filterHandler) {
        Long total = 0L;
        switch (filterHandler.getFilterType()) {
            case platform:
                total = taxiMapper.getActiveTotalCount(null, null);
                break;
            case canton:
                total = taxiMapper.getActiveTotalCount(filterHandler.getCantonIds(), null);
                break;
            case company:
                total = taxiMapper.getActiveTotalCount(null, filterHandler.getCompanyIds());
        }
        // 翻倍率
        Double taxi = filterHandler.getFakeMultiple().getRegisterTotalTaxi();
        if (Objects.nonNull(taxi)) {
            total = (long) (total * taxi);
        }
        return total;
    }

    /**
     * 司机
     */
    private Long getDriverData(FilterHandler filterHandler) {
        Long total = 0L;
        switch (filterHandler.getFilterType()) {
            case platform:
                total = driverMapper.getTotalCount(null, null);
                break;
            case canton:
                total = driverMapper.getTotalCount(filterHandler.getCantonIds(), null);
                break;
            case company:
                total = driverMapper.getTotalCount(null, filterHandler.getCompanyIds());
        }

        // 翻倍率
        Double driver = filterHandler.getFakeMultiple().getRegisterTotalDriver();
        if (Objects.nonNull(driver)) {
            total = (long) (total * driver);
        }

        return total;
    }

    /**
     * 乘客
     */
    private Long getCustomerData(FilterHandler filterHandler) {
        Long total = 0L;
        switch (filterHandler.getFilterType()) {
            case platform:
                total = userMapper.getAllCount(null);
                break;
            case canton:
            case company:
                total = userMapper.getAllCount(filterHandler.getCantonIds());
                break;
        }

        // 翻倍率
        Double ride = filterHandler.getFakeMultiple().getCustomer();
        if (Objects.nonNull(ride)) {
            total = (long) (total * ride);
        }

        return total;
    }
}
